import { _decorator, Component, Node, RigidBody2D, v2, CircleCollider2D, Size, UITransform, v3, clamp, Vec2, Vec3 } from 'cc';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { BasicEnemy, EnemyAlive, EnemyDeath } from './BasicEnemy';
import { Zombie2D } from './Zombie2D';
const { ccclass, property } = _decorator;

@ccclass('People2D')
export class People2D extends BasicEnemy {
    enemyType: GlobalEnum.EnemyTypes = GlobalEnum.EnemyTypes.People;
    private _body: RigidBody2D = null;

    constructor(d) {
        super(d);
        this.mappingProps.enemyType = this.enemyType;
        this.node[GlobalEnum.ExtralProp.curLineVec2d] = v2(0, 1);
        this._body = this.node.getComponent(RigidBody2D);
        this._body.linearVelocity = v2();
    }
    public init(enemyName: string): void {
        super.init();
        this.mappingProps.enemyName = enemyName;
    }
    public setBodySize(s: Size) {
        let trans = this.node.getComponent(UITransform);
        trans.setContentSize(s);

        let body = this.node.getComponent(CircleCollider2D);
        if (body) {
            body.radius = s.width;
            body.apply();
        }
    }
    /**设置初始状态 */
    protected setStateData() {
        this._stateRecs[GlobalEnum.EnemyState.Alive] = new PeopleAlive(this);
        this._stateRecs[GlobalEnum.EnemyState.Death] = new PeopleDeath(this);

        setTimeout(() => {
            this.changeState(GlobalEnum.EnemyState.Alive);
        }, 0);
    }

    public pause() {
        this._body.linearVelocity = v2(0, 0);
    }
    public resume() {

    }
}

export class PeopleAlive extends EnemyAlive {
    private _body: RigidBody2D = null;
    private _lineDirec = v2();
    public checkDist = 0;
    private moveSpd = 0;
    public isRun = false;
    private isShowEmoji = false;
    public _isEndPeople = false;

    public enter(d?: any): void {
        super.enter(d);
        const _cfg = GlobalConfig.People;
        this.checkDist = _cfg[this.cmp.mappingProps.enemyName].checkDist;

        if (!this.cmp.isStatic) {
            const cfg = GlobalConfig.People.moveSpd;
            let lv = StorageSystem.getData().levelAssets.curLv;
            let spd = cfg.min + Math.random() * (cfg.max - cfg.min);
            spd *= 1 + (lv - 1) * cfg.lvRate;

            this.moveSpd = spd;
        } else {
            this.moveSpd = 0;
        }
        this.atkDist = this.checkDist;
        //
        this._outRes.r = this.checkDist;
        this._body = this.cmp.node.getComponent(RigidBody2D);

        //增加属性
        this.cmp.node[GlobalEnum.ExtralProp.PeopleRun] = false;
        this._body[GlobalEnum.ExtralProp.addLineVec] = v2(0);
        //
    }

    public update(dt: any): void {
        if (!this.isRun) {
            this._isEndPeople = this.cmp.node.position.y >= - GlobalTmpData.PartyInfo.partyMark.z / GlobalConfig.Scale2DTo3D;
            if (!this._isEndPeople || this._isEndPeople && GlobalTmpData.PartyInfo.isEnter) {
                this.checkZombies(dt);
            }
        } else {
            this.move(dt);
        }
    }

    womenAudios = [AudioEnum.womenScream1, AudioEnum.womenScream2];
    menAudios = [AudioEnum.manScream1, AudioEnum.manScream2, AudioEnum.manScream3];

    public onChecked(zombie: Zombie2D): void {
        this.isRun = true;
        this.cmp.node[GlobalEnum.ExtralProp.PeopleRun] = !this.cmp.isStatic;
        this._lineDirec.set(0, 1);
        this._isEndPeople = this.cmp.node.position.y >= -GlobalTmpData.PartyInfo.partyMark.z / GlobalConfig.Scale2DTo3D;

        if (this._isEndPeople) {
            //终点线之后的 方向随机
            this._lineDirec.x = (2 * Math.random() - 1);
            this._lineDirec.normalize();
        }

        if (!this.isShowEmoji && !this.cmp.isStatic) {
            this.isShowEmoji = true;
            if (Math.random() < 0.5) {
                //表情
                EventManager.emit(EventTypes.EffectEvents.ShowEmoji,
                    { t: GlobalEnum.EmojiTypes.EnemyFear, target: this.cmp.node });
            }

            //尖叫
            let audio = AudioEnum.womenScream1;
            switch (this.cmp.mappingProps.enemyName) {
                case 'peopleA':
                    audio = this.womenAudios[Math.floor(Math.random() * this.womenAudios.length)];

                    break;
                case 'peopleB':
                case 'peopleC':
                    audio = this.menAudios[Math.floor(Math.random() * this.womenAudios.length)];
                    break;
                default:
                    break;
            }
            AudioSystem.playEffect(audio);
        }

        if (GlobalTmpData.Game.isGuideLv) {
            let d = { t: GlobalEnum.DialogTargetType.people, target: this.cmp.node, isGuide: true, offsetY: 1.5, dialogId: 0 };
            let delay = 0;
            //对话1
            if (this.cmp.isStatic) {
                d.dialogId = 1;
                delay = 600;
            } else {
                d.dialogId = 0;
                delay = 100;
            }
            setTimeout(() => {
                EventManager.emit(EventTypes.UIEvents.ShowDialog, d);
            }, delay);
        }
    }

    private tmpV2 = v2();
    public move(dt) {
        let _scale = GlobalTmpData.Game.isGamePause ? 0 : 1;

        this.tmpV2.set(this._lineDirec).multiplyScalar(dt * this.moveSpd * 60).add(this._body[GlobalEnum.ExtralProp.addLineVec]);
        this.tmpV2.multiplyScalar(_scale);
        this._body.linearVelocity = this.tmpV2;
        this.cmp.node[GlobalEnum.ExtralProp.curLineVec2d].set(this.tmpV2);
    }

}

export class PeopleDeath extends EnemyDeath {
    private _body: RigidBody2D = null;

    public enter(d?: any): void {
        super.enter(d);
        this._body = this.cmp.node.getComponent(RigidBody2D);
        this._body.linearVelocity = v2(0);
    }
    public update(dt: any): void {
        super.update(dt);
    }

    tmpV3 = v3();
    protected onFinish() {
        super.onFinish();
        //奖励
        if (this.isChange) {
            this.tmpV3.set(this.cmp.node.position.x * GlobalConfig.Scale2DTo3D, 1, -this.cmp.node.position.y * GlobalConfig.Scale2DTo3D);
            EventManager.emit(EventTypes.UIEvents.AddGoldEffect, { wpos: this.tmpV3, gold: GlobalConfig.Award.People, isAnim: false });
        }
    }
}